import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';


@Injectable()
export class GoodsManageService {

  constructor(private httpService: HttpService) {
  }

  // 获取分组列表
  getGroupList() {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/list`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取分组详情
  getGroupDetail(id: number) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/` + id + `/info`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 新建分组
  postAddGroup(item: any) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/add`;
    return this.httpService.httpPostAuth(url, item).map(rs => rs);
  }

  // 删除分组
  delGroup(id: number) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/` + id + `/del`;
    return this.httpService.httpPostAuth(url, {}).map(rs => rs);
  }

  // 修改分组
  editGroup(item: any) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/edit`;
    return this.httpService.httpPostAuth(url, item).map(rs => rs);
  }

  // 获取商品列表
  getGoodsList(page: number,
               pageSize: number,
               applet_goods_category_id?: number,
               status?: string, order?: string,
               keyword?: string) {
    const _applet_goods_category_id = applet_goods_category_id || '',
      _status = (status === '选择商品状态' ? '' : status) || '',
      _keyword = keyword || '';
    const _order = (order === 'none' ? '' : order) || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?applet_goods_category_id=${_applet_goods_category_id}` +
      `&order=${_order}&status=${_status}&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品绑定了哪些分类
  getbindListAll(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindListAll?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 商品绑定店铺分类
  goodBindGroup(goods_id, applet_goods_category_id) {
    if (goods_id.length !== 1) {
      // 多个商品绑定单个分类
      const data = {goods_id: JSON.stringify(goods_id), applet_goods_category: applet_goods_category_id[0]},
        url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/goods_bind`;
      return this.httpService.httpPostAuth(url, data).map(rs => rs);
    } else {
      const data = {goods_id: goods_id[0], applet_goods_category: JSON.stringify(applet_goods_category_id)},
        url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bind`;
      return this.httpService.httpPostAuth(url, data).map(rs => rs);
    }
    // else if (goods_id.length === 1 && applet_goods_category_id.length !== 1) {
    //   // 单个商品绑定多个分类
    //   const data = {goods_id: goods_id[0], applet_goods_category: JSON.stringify(applet_goods_category_id)},
    //     url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bind`;
    //   return this.httpService.httpPostAuth(url, data).map(rs => rs);
    // } else {
    //   // 单个商品绑定单个分类
    //   const data = {goods_id: goods_id[0], applet_goods_category: applet_goods_category_id[0]},
    //     url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/single_bind`;
    //   return this.httpService.httpPostAuth(url, data).map(rs => rs);
    // }
  }

  // 商品解绑分类  一对一
  goodsUnbindGroup(data: any) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/unbind`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }
}
